package com.hourse.platform.service.impl;

import com.hourse.platform.entity.LogEntity;
import com.hourse.platform.service.AutoLogService;
import com.hourse.app.utils.DateUtil;
import com.hourse.platform.vo.AutoLogQueryVo;
import com.hourse.platform.vo.PageList;
import com.hourse.platform.vo.RoleVO;
import com.hourse.platform.entity.UserEntity;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.RoleService;
import com.hourse.platform.service.UserService;
import com.hourse.platform.utils.AppTools;
import com.hourse.platform.utils.SqlParams;
import com.hourse.platform.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AutoLogServiceImpl extends PublicServiceImpl implements AutoLogService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    /**
     * 操作日志记录方法
     *
     * @param logContent
     * @param logType
     * @param operatetype
     * @throws AppException
     */
    @Override
    @Transactional
    public void addLog(String logContent, Integer logType, Integer operatetype) throws AppException {

        LogEntity logEntity = new LogEntity();

        //注解上的描述,操作日志内容
        logEntity.setLogContent(logContent);
        logEntity.setLogType(logType);
        logEntity.setOperateType(operatetype);

        UserEntity userEntity = userService.getUserById(AppTools.GetCurrentUserId());
        if (userEntity != null) {
            logEntity.setUserid(AppTools.GetCurrentUserId());
            logEntity.setUsername(userEntity.getName());

            ArrayList<String> roles = new ArrayList<>();
            List<RoleVO> roleList = userService.getRoleList(AppTools.GetCurrentUserId());
            for (RoleVO roleVO : roleList) {
                if (roleVO.getAuth().equals("1")) {
                    roles.add(roleVO.getName());
                }
            }
            logEntity.setUserRole(roles.toString());

        }

        this.saveOrUpdate(logEntity);
    }

    /**
     * 查询操作日志
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional
    public PageList findAll(AutoLogQueryVo vo) throws ParseException {

        SqlParams sqlParams = new SqlParams();

        sqlParams.append("select * from log where 1 = 1");

        if (ObjectUtils.isEmpty(vo.getLogType())) {
            sqlParams.append(" and log_type = :logType");
            sqlParams.add("logType", vo.getLogType());
        }

        if (ObjectUtils.isEmpty(vo.getOperateType())) {
            sqlParams.append(" and operate_type = :operateType");
            sqlParams.add("operateType", vo.getOperateType());
        }

        if (StringTools.isNotNull(vo.getUsername())) {
            sqlParams.append(" and username like CONCAT('%',:username,'%')");
            sqlParams.add("username", vo.getUsername());
        }

        if (StringTools.isNotNull(vo.getEpName())) {
            sqlParams.append(" and ep_name like CONCAT('%',:epName,'%')");
            sqlParams.add("epName", vo.getEpName());
        }

        if (StringTools.isNotNull(vo.getQueryTime())) {
            sqlParams.append(" and DATE_FORMAT(create_time, \"%Y-%m-%d\") = :startTime");
            sqlParams.add("startTime",
                    DateUtil.DateToString(DateUtil.StringToDate(vo.getQueryTime()), DateUtil.ptn_10));
        }

        sqlParams.append(" order by create_time desc");

        if (ObjectUtils.isEmpty(vo.getStart()) || vo.getStart() <= 0) {
            vo.setStart(1);
        }
        if (ObjectUtils.isEmpty(vo.getSize()) || vo.getSize() <= 0) {
            vo.setSize(10);
        }


        PageList pageList = baseDao.getPageListByNativeSQL(sqlParams.getSql(), sqlParams.getParams(), vo.getStart(), vo.getSize());

        return pageList;
    }

}
